<template>
  <div>
    <template v-if="listData.length > 0">
      <el-row :gutter="24">
        <el-col class="mb20" v-for="(item,index) in listData" :key="index" :span="8">
          <el-card shadow="never" class="list-card">
            <div slot="header" style="text-align: center">
              <span>{{item.evalPlanName}}</span>
              <el-tooltip class="item" effect="dark" content="查看详情" placement="top">
                <i
                  @click.stop="handleDetail(item)"
                  v-if="$auth('eval:detail')"
                  class="fr curp fs16 el-icon-document"
                />
              </el-tooltip>
            </div>
            <el-form label-width="130px" label-suffix="：">
              <el-form-item label="计划周期">
                <span>{{ judgeRange(item.evalPlanCycle) }}</span>
              </el-form-item>
              <el-form-item label="计划日期">
                <span>{{ item.startDate ? `${item.startDate}` : null }}{{ item.endDate ? `至${item.endDate}` : null }}</span>
              </el-form-item>
              <el-form-item label="创建人">{{ item.createUser }}</el-form-item>
              <el-form-item label="创建时间">{{ formatTime(item.createTime) }}</el-form-item>
              <el-form-item label="考评形式">{{ item.evalMode === 1 ? '填报' : '巡查' }}</el-form-item>
              <el-form-item label="计划状态">
                <span>
                  <el-tag :type="judgeType(item.state)">{{ judgeState(item.state) }}</el-tag>
                </span>
              </el-form-item>
              <el-form-item
                label="考核对象形式"
              >{{ item.targetMode === 1 ? '指定部门' : ( item.targetMode === 2 ? '指定个人' : '全部人员' ) }}</el-form-item>
              <el-form-item label="执行对象数">{{item.targetNum}}</el-form-item>
              <el-form-item label="要求对象执行数">{{item.execNum}}</el-form-item>
              <el-form-item label="完成执行对象数">{{item.performNum}}</el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col class="tl-center nomoreWord" v-show="noMore">---- 这是我的底线 ----</el-col>
      </el-row>
    </template>
    <template v-else>
      <empty-comp ref="emptyComp" />
    </template>
  </div>
</template>

<script>
import { finishedTask } from '@/api/eval'
import { judgeRange, judgeEvalState, judgeType } from '@/utils/util'

export default {
  props: {
    name: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      listData: [],
      total: 1,
      queryParam: {
        pageSize: 10,
        pageNo: 1,
        permissionsType: 1
      },
      noMore: false
    }
  },
  watch: {
    name(n, o) {
      this.name = n
    }
  },
  mounted() {
    this.getFinishedTask()
    window.addEventListener('scroll', this.getScroll)
  },
  destroyed() {
    window.removeEventListener('scroll', this.getScroll)
  },
  methods: {
    getFinishedTask(param = {}) {
      this.queryParam = Object.assign(this.queryParam, param)
      finishedTask(this.queryParam).then(res => {
        if (res && res.data.records.length > 0) {
          this.listData = [...this.listData, ...res.data.records]
          this.total = res.data.total
          if (this.queryParam.pageNo * 10 > this.total) {
            this.noMore = true
          }
        } else {
          this.listData = []
          this.total = 1
        }
      })
    },
    getScroll() {
      if (this.name === 'second') {
        if (document.documentElement.scrollTop + document.documentElement.clientHeight >= document.documentElement.scrollHeight) {
          this.loadMore()
        }
      }
    },
    loadMore() {
      console.log('loadMore', this.listData.length, this.total)

      if (this.queryParam.pageNo * 10 > this.total) {
        this.noMore = true
      } else {
        this.queryParam.pageNo += 1
        this.getFinishedTask(this.queryParam)
      }
    },
    judgeRange(num) {
      return judgeRange(num)
    },
    judgeType(num) {
      return judgeType(num)
    },
    judgeState(num) {
      return judgeEvalState(num)
    },
    handleDetail(row) {
      this.$router.push({
        // name: 'evaluateFinishDetail',
        name: 'evaluateTable',
        params: { row }
      })
    },
    handleEdit(bool, row) {
      console.log(bool, row)
    }
  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 10px;
}
</style>